// index.js
// 获取应用实例
import {categoryList,adPage } from '../../common/api/index';
import dayjs from 'dayjs';
import {
  knowPage,
  
} from '../../common/api/shop.js';
const app = getApp()

Page({
  data: {
    navBarHeight: getApp().globalData.navBarHeight,
    statusBarHeight:getApp().globalData.statusBarHeight,
    tabData:[],
    activeIndex:0,
    goodsData:[13,3,3,3,3,3,],
    commodityArr: [],
    page: 1, // 页码
    goodsName: '', // 搜索名字
    bottomLoading: true,
    refresherTriggered: false,
    adList: [],
  },
 
  onLoad() {
    this.getCategory();
    this.getAdPage();
    
  },

  // 商品倒计时借宿
  finish(e){
    // console.log('倒计时结束',e.currentTarget.dataset.item)
    let row = e.currentTarget.dataset.item;
    let commodityArr = JSON.parse(JSON.stringify(this.data.commodityArr))
    commodityArr.forEach((e,index) =>{
      if(row.id === e.id){
        e.end = true;
        let keys = "commodityArr[" + index + "]";
        this.setData({
          [keys]: e
        });

      }
    });
    

  },
  onShareAppMessage(e){
    let user = wx.getStorageSync('userInfo');
    console.log(e);
    return {
      title: "杆赢天下，求胜未来。一元乐享好球杆，千万球友的选择",
      imageUrl: "../../images/shareImg.jpg",
       path: "/pages/index/index?id=" + user.id
    }
  },
  getCategory: async function () {
    // const vm = this;
    // utils.get("category/list").then((res)=>{
    //   vm.setData({
    //     categoryData:res
    //   })
    // })
    let res = await categoryList({});
    this.setData({
      tabData: res
    });
    console.log('事件名,getCategory')
    // 请求商品；
    this.getShop();
  },
    /**
   * 搜索
   */
  getSeacrh: function(){
    this.bindreFresherrefresh()
   },
   /**
   * 获取商品列表
   */
  getShop: async function () {
    let data = {
      categoryId : this.data.tabData[this.data.activeIndex].id,
      type: '',
      page: this.data.page,
      size: 10,
      goodsName: this.data.goodsName,
      isOnSale: '1',
      isBuy: '0',
      isNew: 1
    };
    let res = await knowPage(data);
    console.log(res);
    // console.log(res.list, '商品列表')
    // 测试分页
    // let arr2 = [];
    // for(let i=0;i<10;i++){
    //   arr2.push(...res.list)
    // };
    // let arr = arr2 || [];
  //   res.list = [
  //     {
  //         "brandId": 3,
  //         "brandName": "中国黄金",
  //         "brief": null,
  //         "categoryId": 18,
  //         "categoryName": "钟表首饰",
  //         "counterPrice": 1999,
  //         "createTime": 1691111303000,
  //         "detail": "<p><img src=\"https://yulemen.fun/api/static/images/614dd03ac83a46fba0c78e5907f477cc1.jpg\"><img src=\"https://yulemen.fun/api/static/images/85658ed1db864e0e9ff86b51949cecb42.jpg\"><img src=\"https://yulemen.fun/api/static/images/b7e70903da9241f58d013fe309689b263.jpg\"><img src=\"https://yulemen.fun/api/static/images/07405e6e672249e19ceac2b292113f364.jpg\"><img src=\"https://yulemen.fun/api/static/images/e46136916ce94041b9af0fdb5592a0615.jpg\"><img src=\"https://yulemen.fun/api/static/images/09b015ac88a54df4942974bc8304dcd86.jpg\"></p>",
  //         "differPrice": 0,
  //         "gallery": "/static/images/d8d70aedf85342819143560c54eb546b2.jpg,/static/images/85ea52804cf24a6a86e6ad5a7ffea33b3.jpg,/static/images/3514ad79f25a460a965d3b3a94c68e844.jpg",
  //         "galleryUrls": [
  //             "https://yulemen.fun/api/static/images/d8d70aedf85342819143560c54eb546b2.jpg",
  //             "https://yulemen.fun/api/static/images/85ea52804cf24a6a86e6ad5a7ffea33b3.jpg",
  //             "https://yulemen.fun/api/static/images/3514ad79f25a460a965d3b3a94c68e844.jpg"
  //         ],
  //         "gallerys": [
  //             "/static/images/d8d70aedf85342819143560c54eb546b2.jpg",
  //             "/static/images/85ea52804cf24a6a86e6ad5a7ffea33b3.jpg",
  //             "/static/images/3514ad79f25a460a965d3b3a94c68e844.jpg"
  //         ],
  //         "goodsName": " 金条",
  //         "goodsSn": " D116-3006",
  //         "historys": null,
  //         "id": 10,
  //         "isBuy": 0,
  //         "isOnSale": 1,
  //         "keywords": " 金条",
  //         "picUrl": "/static/images/babfb084ae874fefbb60eeaddf78a1e31.jpg",
  //         "picUrlSrc": "https://yulemen.fun/api/static/images/babfb084ae874fefbb60eeaddf78a1e31.jpg",
  //         "records": null,
  //         "retailPrice": 1,
  //         "second": 3,
  //         "sortOrder": 1,
  //         "status": 1,
  //         "totalPrice": 1999,
  //         "totalUser": 0,
  //         "unit": "条"
  //     }
  // ];
    res.list.forEach(e =>{
      e.winTime2 = dayjs(e.winTime).format('YYYY-MM-DD HH:mm')
    })
    let arr = res.list || [];
      if(arr.length < data.size){
        this.setData({
          bottomLoading: false
        })
      }
    let a = [...this.data.commodityArr, ...arr];
    this.setData({
      commodityArr: a,
      refresherTriggered: false
    });
  },
  // 
  bindBottomLoading(){
    if(!this.data.bottomLoading){
      console.log('已经没有了')
      return
    }
    this.data.page+=1;
    console.log('事件名,bindBottomLoading')
    this.getShop()
  },
  bindreFresherrefresh(){
    this.data.page = 1;
    this.setData({
      bottomLoading: true,
      commodityArr: []
    });
    console.log('事件名,bindreFresherrefresh')
    this.getShop();
  },
  inputChange: function(e){
    this.data.goodsName = e.detail.value;
  },
  // 切换
  swiperBindchange(e){
      console.log(e.detail.current, '切换')
      this.setData({
        activeIndex: e.detail.current
      });
      this.bindreFresherrefresh();
  },
  changeTab:function(e){
    this.setData({
      activeIndex:e.currentTarget.dataset.index
    });
    // this.bindreFresherrefresh()
  },
  /**
   * 请求广告
   * */ 
   async getAdPage(){
    let res = await adPage({size: 10, page: 1,title: ''})
    this.setData({
      adList: res.list
    })
  },
  go: function (e) {
    wx.navigateTo({
      url: '/pages/allShop/shopDetail/shopDetail?id='+ e.currentTarget.dataset.item.id,
      // url: '../shopDetail/shopDetail?id='+ e.currentTarget.dataset.item.id,
    })
  },
  go2: function (e) {
    let type = 1;
    if(e.currentTarget.dataset.item.isGold==1){
      type=3;
    }
    wx.navigateTo({
      url: '/pages/allShop/shopDetail/shopDetail?id='+ e.currentTarget.dataset.item.id + '&type='+type,
      // url: '../shopDetail/shopDetail?id='+ e.currentTarget.dataset.item.id,
    })
  },


  // 预览
  preview(e){
    console.log()
    let item = e.currentTarget.dataset.item;
    // console.log(item.urlSrc)
    wx.previewImage({
      urls: [item.urlSrc],
    })
  },
  
})
